<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\House;

class HouseController extends Controller

{
    
    public function add(Request $request){
        $arr = json_decode($request->form,true);
        $data = House::create($arr);
        return response()->json(['meta'=>['msg'=>'房源发布成功!','status'=>200],'data'=>$data]);
    }


    public function create(Request $request){

        //return response()->json(['msg'=>$request->file("pic")]);

        // 1 先上传3张图片;
        $pics = $request->file("pic");
        

        $path = \base_path("/public/upload/");
        $picStr = "";

        foreach($pics as $k=>$file){
             $ext = $file->getClientOriginalExtension();
             $picName = time().rand(0,999).".".$ext;
             $file->move($path,$picName);

             $picStr .= "&".$picName;
             if($k==0){
                 $this->imageResize($path,$picName,200,160);
             }
        }

        // 2 将其他表单数据转为php数组;
        $arr = json_decode($request->form,true);

        // 3 把图片字符串保存到数组中;
        $arr["imgs"] = ltrim($picStr,"&");  // XXX.jpg&XXX.jpg&xxx.jpg

        // 4 表单数据(保护图片信息)
        $data = House::create($arr);
        
        return response()->json(['meta'=>['status'=>200,'msg'=>'房源发布成功!'],'data'=>$data]);
         
    }


    // 更新房源信息 
    public function save_edit(Request $request){

        // 1 先上传3张图片; 
        $pics = $request->file("pic");
        $path = \base_path("/public/upload/");
        $picStr = "";

        foreach($pics as $k=>$file){
             $ext = $file->getClientOriginalExtension();
             $picName = time().rand(0,999).".".$ext;
             $file->move($path,$picName);
             $picStr .= "&".$picName;
             if($k==0){
                 $this->imageResize($path,$picName,200,160);
             }
        }

        // 2 将其他表单数据转为php数组;
        $arr = json_decode($request->form,true);

        // 4 表单数据(保存图片信息)  
        $house = House::find($arr['id']);
        $house->title = $arr['title'];
        $house->des = $arr['des'];
        $house->nick_name = $arr['nick_name'];
        $house->tel = $arr['tel'];
        $house->imgs = ltrim($house->imgs.$picStr,"&");

        $bool = $house->save();

        if($bool){
            return response()->json(['meta'=>['status'=>200,'msg'=>'房源更新成功!'],'data'=>$house]);
        }else{
            return response()->json(['meta'=>['status'=>3000,'msg'=>'房源更新失败!'],'data'=>$house]);
        }
        
    }

private function imageResize($path, $picname, $maxW=100, $maxH=100, $pre='s_')

{

    //2.去除path路径右侧的 /，再加上
    $path = rtrim($path,'/').'/';

    //3.获取一张图片详细信息
    $info = getimagesize($path.$picname);

    //4.根据用户上传的图像的类型，来判断我们需要用哪一个函数生成画布(imagejpeg、imagepng、imagegif)
    switch($info[2]){
        case "1":   //gif
            $oldImg = imagecreatefromgif($path.$picname);
            break;

        case "2":   //jpg
            $oldImg = imagecreatefromjpeg($path.$picname);
            break;

        case "3":   //png
            $oldImg = imagecreatefrompng($path.$picname);
            break;
    }

    //5.获取原图的宽高
    $oldW = imagesx($oldImg);
    $oldH = imagesy($oldImg);

    //6.判断原图的宽高那个边比较长
    if($oldW > $oldH){

        //计算比例
        $b = $oldW / $maxW;

        //计算缩放后的新的宽度与高度
        $newW = $oldW / $b;
        $newH = $oldH / $b;

    }else{

        //计算比例
        $b = $oldH / $maxH;

        //计算缩放后的新的宽度与高度
        $newW = $oldW / $b;
        $newH = $oldH / $b;

    }

    //7.根据缩放后的宽度与高度，生成一个新的画布
    $newImg = imagecreatetruecolor($newW, $newH);

    //8.开始缩放
    imagecopyresampled($newImg, $oldImg, '0','0','0','0',$newW,$newH,$oldW,$oldH);

    //9.根据原图的类型，生成对应的图像
    switch($info[2]){
        case "1":
            imagegif($newImg, $path.$pre.$picname); //./Uplods/s_123.jpg
            break;

        case "2":
            imagejpeg($newImg, $path.$pre.$picname);
            break;

        case "3":
            imagepng($newImg, $path.$pre.$picname);
            break;
    }

    //10.释放资源
    imagedestroy($oldImg);
    imagedestroy($newImg);
   }

   // 删除图片
   public function del_img(Request $request){

    // 根据id查找房源
    $house = House::find($request->id);

    $imgArr = explode("&",$house->imgs);

    // 上传图片路径
    $imgPath = base_path('public/upload/');
    
   
    $file = $imgPath.$request->img;
    $small_file = $imgPath.'s_'.$request->img;

    // 删除大图 
    if(file_exists($file)){
        unlink($file);
    }

    // 删除小图
    if(file_exists($small_file)){
        unlink($small_file);
    }
    
    $index = $this->getImgIndex($request->img,$imgArr);
    
    // 数据库删除;
    array_splice($imgArr, $index, 1);
    $house->imgs = implode('&',$imgArr);
    $bool = $house->save();

    // 返回数据
    if($bool){
        return response()->json(['meta'=>['msg'=>'删除成功!','status'=>200,'file'=>$file]]);
    }else{
        return response()->json(['meta'=>['msg'=>'删除失败!','status'=>3000]]);
    }


  }

  // 获取当前图片索引值
  private function getImgIndex($img,$imgArr){
       foreach($imgArr as $index => $val){
           if($img == $val)
              return $index;
       }
  }



}
